﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace exsportance.Silverlight.Forms.Controls
{
    /// <summary>
    /// 
    /// </summary>
    ///  <remarks>http://programmerpayback.com/2008/11/26/silverlight-groupbox-control/</remarks>
    public class GroupBox : ContentControl
    {
        #region members
        private RectangleGeometry _fullRect;
        private RectangleGeometry _headerRect;
        private ContentControl _headerContainer;
        #endregion

        #region properties
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(GroupBox), null);
        public object Header
        {
            get { return GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(GroupBox), null);
        public DataTemplate HeaderTemplate
        {
            get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
            set { SetValue(HeaderTemplateProperty, value); }
        }
        #endregion

        #region constructor
        /// <summary>
        /// Creates a new group box
        /// </summary>
        public GroupBox()
        {
            DefaultStyleKey = typeof(GroupBox);
            this.SizeChanged += GroupBox_SizeChanged;
        }
        #endregion

        #region public methids
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _fullRect = (RectangleGeometry)GetTemplateChild("FullRect");
            _headerRect = (RectangleGeometry)GetTemplateChild("HeaderRect");
            _headerContainer = (ContentControl)GetTemplateChild("HeaderContainer");
            _headerContainer.SizeChanged += HeaderContainer_SizeChanged;
        }
        #endregion

        #region private methods
        private void GroupBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            _fullRect.Rect = new Rect(new Point(), e.NewSize);
        }

        private void HeaderContainer_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            _headerRect.Rect = new Rect(new Point(_headerContainer.Margin.Left, 0), e.NewSize);
        }
        #endregion
    }
}
